package com.kichina.parking.controller.base;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.validation.FieldError;

import java.io.Serializable;
import java.util.List;

/**
 * @author Surging
 * @version 1.0
 * @ClassName IMessageHandler
 * @date 2022-06-16 14:01
 * @desc  控制层数据处理成功返回或者失败返回
 *   {
 *     "service":  "uploadcarin",
 *     "result_code": 0 ,
 *     "message": "上传成功",
 *     "order_id": "10001"
 *   }
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class BaseUpstreamResponse implements Serializable {

    private String service;
    private String order_id;
    private String orderid;
    private String message;
    private Integer result_code;
    private Integer mobilechargeid;
    private List<FieldError> fieldErrorList;
    private String recordid;
    private String rule_id;

    public BaseUpstreamResponse(String service, String message, Integer result_code, String recordid) {
        this.service = service;
        this.message = message;
        this.result_code = result_code;
        this.recordid = recordid;
    }

    public BaseUpstreamResponse(String service, String order_id, String message, int result_code) {
        this.service = service;
        this.order_id = order_id;
        this.message = message;
        this.result_code = result_code;
    }

    public BaseUpstreamResponse(int result_code, String service,  String message, String rule_id) {
        this.result_code = result_code;
        this.service = service;
        this.rule_id = rule_id;
        this.message = message;

    }

    public BaseUpstreamResponse(String service, String orderid, String message, int result_code, String order_id) {
        this.service = service;
        this.orderid = orderid;
        this.message = message;
        this.result_code = result_code;
        this.order_id = order_id;
    }

    public BaseUpstreamResponse(String service, String message, int result_code) {
        this.service = service;
        this.message = message;
        this.result_code = result_code;
    }

    public BaseUpstreamResponse(String service, String order_id) {
        this.service = service;
        this.order_id = order_id;
    }

    public BaseUpstreamResponse(String service, int result_code) {
        this.service = service;
        this.result_code = result_code;
    }

    public BaseUpstreamResponse(String service, String order_id, String message, Integer result_code, Integer mobilechargeid) {
        this.service = service;
        this.order_id = order_id;
        this.message = message;
        this.result_code = result_code;
        this.mobilechargeid = mobilechargeid;
    }

    public BaseUpstreamResponse(String service) {
        this.service = service;
    }

    public BaseUpstreamResponse() {
    }

    public static BaseUpstreamResponse buildSuccess(String service) {
        return new BaseUpstreamResponse(service, "上传成功", 0);
    }

    public static BaseUpstreamResponse buildSuccess(String service, String order_id, Integer mobilechargeid) {
        return new BaseUpstreamResponse(service, order_id,"上传成功",0 , mobilechargeid);
    }


    public static BaseUpstreamResponse buildSuccess(String service, String order_id) {
        return new BaseUpstreamResponse(service, order_id, "上传成功", 0);
    }

    public static BaseUpstreamResponse buildSuccessRecordId(String service, String recordId) {
        return new BaseUpstreamResponse(service, "上传成功", 0, recordId);
    }

    public static BaseUpstreamResponse buildSuccessRuleId(String service, String ruleId) {
        return new BaseUpstreamResponse(0, service, "上传成功",  ruleId);
    }

    public static BaseUpstreamResponse buildFailed(String service, String order_id) {
        return new BaseUpstreamResponse(service, order_id, "上传失败", 1);
    }

    public static BaseUpstreamResponse buildFailed(String service, String order_id, Integer mobilechargeid) {
        return new BaseUpstreamResponse(service, order_id, "上传失败", 1, mobilechargeid);
    }

    public static BaseUpstreamResponse buildFailedRecordId(String service, String recordId) {
        return new BaseUpstreamResponse(service,"上传失败", 1, recordId);
    }

    public static BaseUpstreamResponse buildFailedRuleId(String service, String ruleId) {
        return new BaseUpstreamResponse(1, service,"上传失败",  ruleId);
    }

    public static BaseUpstreamResponse buildFailed(String service) {
        return new BaseUpstreamResponse(service, "上传失败", 1);
    }

    public static BaseUpstreamResponse buildSuccessOrderId(String service, String orderid) {
        return new BaseUpstreamResponse(service, orderid, "上传成功", 0, null);
    }

    public static BaseUpstreamResponse buildFailedOrderId(String service, String orderid) {
        return new BaseUpstreamResponse(service, orderid, "上传失败", 1, null);
    }


    public static BaseUpstreamResponse buildFailed() {
        return new BaseUpstreamResponse("上传失败", 1);
    }

    public static BaseUpstreamResponse buildSuccess() {
        return new BaseUpstreamResponse("上传成功", 1);
    }


    public static <T extends BaseUpstreamResponse> T buildFailed(Class<T> clazz, String code, String message,
                                                                 List<FieldError> fieldErrorList) {
        try {
            T t = clazz.newInstance();
            t.setOrder_id(code);
            t.setMessage(message);
            t.setFieldErrorList(fieldErrorList);
            return t;
        } catch (Exception e) {
            throw new RuntimeException(
                    "Build failed base response error. " + clazz.getSimpleName() + " " + code + " " + message);
        }
    }

    public static <T extends BaseUpstreamResponse> T buildFailed(Class<T> clazz, String code, String message) {
        return buildFailed(clazz, code, message, null);
    }




    public void setFieldErrorList(List<FieldError> fieldErrorList) {
        this.fieldErrorList = fieldErrorList;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getOrder_id() {
        return order_id;
    }

    public void setOrder_id(String order_id) {
        this.order_id = order_id;
    }

    public String getOrderid() {
        return orderid;
    }

    public void setOrderid(String orderid) {
        this.orderid = orderid;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getResult_code() {
        return result_code;
    }

    public void setResult_code(Integer result_code) {
        this.result_code = result_code;
    }

    public Integer getMobilechargeid() {
        return mobilechargeid;
    }

    public void setMobilechargeid(Integer mobilechargeid) {
        this.mobilechargeid = mobilechargeid;
    }

    public List<FieldError> getFieldErrorList() {
        return fieldErrorList;
    }

    public String getRecordid() {
        return recordid;
    }

    public void setRecordid(String recordid) {
        this.recordid = recordid;
    }

    public String getRule_id() {
        return rule_id;
    }

    public void setRule_id(String rule_id) {
        this.rule_id = rule_id;
    }

    @Override
    public String toString() {
        return "BaseUpstreamResponse{" +
                "service='" + service + '\'' +
                ", order_id='" + order_id + '\'' +
                ", orderid='" + orderid + '\'' +
                ", message='" + message + '\'' +
                ", result_code=" + result_code +
                ", mobilechargeid=" + mobilechargeid +
                ", fieldErrorList=" + fieldErrorList +
                ", recordid='" + recordid + '\'' +
                ", rule_id='" + rule_id + '\'' +
                '}';
    }
}

